/**
 * 
 */
package fr.toussna.utils;

import java.util.Random;

import fr.toussna.entity.Article;
import fr.toussna.entity.Category;
import fr.toussna.entity.Comment;
import fr.toussna.entity.DomainEntity;
import fr.toussna.entity.Member;

/**
 * Renvoit des objets metiers a des fins de tests.
 * 
 * @author My
 * 
 */
public class EntityFactory {

	/**
	 * Cette methode instancie des objets a la demande
	 * C'est une methode recursive
	 * 
	 * @param name
	 * @return {@link DomainEntity}
	 */
	public  DomainEntity createDomainEntity(String name) {
		DomainEntity entity;
		String id = getUniqueId();
		if (name.equals("article")) {
			entity = new Article("date" + id,
			        (Category) createDomainEntity("category"),
			        (Member) createDomainEntity("member"), "title" + id,
			        "content" + id);
			return entity;
		} else if (name.equals("category")) {
			entity = new Category("category" + id);
			return entity;
		} else if (name.equals("member")) {
			entity = new Member("email" + id, "pwd" + id, "name" + id);
			return entity;
		} else if (name.equals("comment")) {
			entity = new Comment((Article) createDomainEntity("article"),
			        "title" + id);
			return entity;
		} else {
			return null;
		}
	}

	/**
	 * Cette methode instancie des objets a la demande
	 * 
	 * @param name
	 * @return {@link DomainEntity}
	 */
	public  DomainEntity createDomainEntity(String name, int idCat,int idMem) {
		DomainEntity entity;
		String id = getUniqueId();
		entity = new Article("date" + id, new Category(idCat),
		        new Member(idMem), "title" + id, "content" + id);
		return entity;

	}

	/**
	 * 
	 */
	private static String getUniqueId() {
		return String.valueOf(((int)(Math.random() * 2000)));
	}
}
